﻿/**
 * ...
 * @author Артём Григорьев
 */

package loader;

import api.DAO;
import client.Channel;
import client.Item;
import client.ReceivedItem;
import client.Category;
import api.ServerApi;
import flash.events.TextEvent;

class Loader extends ServerApi
{
	private static inline var test_mode : Bool	 = true;
	private static inline var serverURL : String = "http://naadb.avafreehost.com/";
	private static inline var authKey   : String = Application.getAuthKey();
	private static inline var appName   : String = "News Aggregating Agent";
	private static inline var apiSecret : String = "vmNGBBxinj";
	private static inline var apiID		: Int 	 = 1905904;
	//private static var userId		    : Int    = Application.getUserId();
	private static var viewerId			: Int	 = Application.getViewerId();
	//private static inline var curId	: Int    = currentId(userId, viewerId);
	private static var inst 		    : Loader;
	public static var apiRequest 		: DAO;
	
	
	public static function createInstance() {
		if (inst == null) inst = new Loader();
	}
	
	public static function getInstance() {
		return inst;
	}
	
	private static  function currentId(uId : Int, vId : Int):Int {
		var Id = (uId == 0)? uId : vId;
		return Id;
	}
	
	
	public static function wrapImageLink(url : String) {
//		trace(serverURL + "index.php?method=load_picture&viewer_id=" + viewerId + "&app=" + appName + "&auth_key=" + authKey + "&url=" + url);
		return serverURL + "index.php?method=load_picture&viewer_id=" + viewerId + "&app=" + appName + "&auth_key=" + authKey + "&url=" + url;
	}
	
	public function new() 
	{
		//trace (authKey);
		super(serverURL, authKey, viewerId, appName);
		
		apiRequest = new DAO(apiID, apiSecret, Application.getViewerId(), test_mode);
		//trace(Application.getViewerId());
	}
	
	public function getChannels(onLoad : Array<Channel> -> Void)
	{
		var params = {
		//	method: "get_channels"
		};
		
		var onChannelsLoaded = function(xml : Xml) {
			var parser = new ChannelXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("get_channels", params, onChannelsLoaded, onError);
	}
	public function AddToFavorites(id : Int) {
		var params = {
		//	method : "add_to_favorites",
			channel : id
		};
		
		var stub = function (xml : Xml) {
		};
		
		this.request("add_to_favorites", params, stub, onError);
	}
	
	public  function getFavorites(onLoad : Array<Channel> -> Void) {
		var params = {
		//	method: "get_favorites"
		};
		
		var onChannelsLoaded = function(xml : Xml) {
			var parser = new ChannelXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("get_favorites", params, onChannelsLoaded, onError);
	}
	
	public  function getIDFavorites(onLoad : Array<Int> -> Void) {
		var params = {
		//	method: "get_favorites"
		};
		
		var onChannelsLoaded = function(xml : Xml) {
			var parser = new ChannelXmlParser(xml);
			onLoad(parser.parseID());
		};
		
		this.request("get_favorites", params, onChannelsLoaded, onError);
	}
	
	public function getChannelsByCategory(onLoad : Array<Channel> -> Void, categoryId : UInt)
	{
		var params = {
		//	method: "chtagsearch",
			tag: categoryId
		};
		
		
		var onChannelsLoaded = function(xml : Xml) {
			var parser = new ChannelXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("chtagsearch", params, onChannelsLoaded, onError);
	}
	public function getItemsByCategory(onLoad : Array<Item> -> Void, categoryId : UInt)
	{
		var params = {
			//method: "tagsearch",
			tag: categoryId
		};
		
		
		var onItemsLoaded = function(xml : Xml) {
			var parser = new ItemXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("tagsearch", params, onItemsLoaded, onError);
	}
	
	public function getCategory(onLoad : Array<Category> -> Void)
	{
		var params = {
		//	method: "get_category"
		};
		
		var onCategoriesLoaded = function(xml : Xml) {
			var parser = new CategoryXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("get_category", params, onCategoriesLoaded, onError);
	}
	
	public function getItems(onLoad : Array<Item> -> Void, channelId : UInt, limit : UInt)
	{
		var params = {
			//method: "get_items",
			ch_id: channelId,
			limit : limit
		};
		
		var onItemsLoaded = function(xml : Xml) {
			var parser = new ItemXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("get_items", params, onItemsLoaded, onError);
	}
	
	public function sendMessage(onLoad : Array<Item> -> Void, receiver : Int, item : Int) {
		var params = {
			//method : "send_message",
			receiver : receiver,
			item : item
		}
		
		var stub = function (xml : Xml) {
			
		}
		this.request("send_message", params, stub, onError);
	}
	
	public function readMessages(onLoad : Array<ReceivedItem> -> Void) {
		var params = {
		//	method : "read_messages"
		};
		
		var onMessagesLoaded = function(xml : Xml) {
			var parser = new ReceivedItemXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("read_messages", params, onMessagesLoaded, onError);
	}
		
	
	public function messagesToRead(onLoad : Int -> Void) {
		var params = {
		//	method : "messages_to_read"
		};
		
		var onResult = function(xml : Xml) {
			var iter = xml.iterator();
			var fc = xml.elementsNamed("response");
			var resp = fc.next();
			onLoad(Std.parseInt(cast(resp.firstChild().firstChild())));
		}
		
		this.request("messages_to_read", params, onResult, onError);
	}	
	
	public function readMessage(message : Int) {
		var params = {
		//	method : "read_message",
			message : message
		};
		
		var stub = function (xml : Xml) {
			
		}
		this.request("read_message", params, stub, onError);		
	}
	
	public function deleteMessage(id : Int) {
		var params = {
		//	method : "delete_message",
			message : id
		};
		
		var stub = function (xml : Xml) {
			
		}
		this.request("delete_message", params, stub, onError);		
	}
	
	
	public function FindTag(onLoad : Array<Channel> -> Void, catlist : String) {
		var params = {
			//method: "Findchatag", tag: catlist
		};
		var onChannelsLoaded = function(xml : Xml) {
			var parser = new ChannelXmlParser(xml);
			onLoad(parser.parse());
		};
		this.request("Findchatag", params, onChannelsLoaded, onError);
	}
	
		
			
			
	public function FindItemTag(onLoad : Array<Item> -> Void, catlist : String) {
		var params = {
		//	method: "Finditemtag", tag: catlist
		};
		
		var onItemsLoaded = function(xml : Xml) {
			var parser = new ItemXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("Finditemtag", params, onItemsLoaded, onError);	
		}	
	
	public function getSearch( onLoad : Array<Item> -> Void , text: String ) {
		var params = {
			//method: "search",
			word: text
		};
		
		var onItemsLoaded = function(xml : Xml) {
			var parser = new ItemXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("search", params, onItemsLoaded, onError);
	}
	
	public function searchInFavorites( onLoad : Array<Item> -> Void , text: String ) {
		var params = {
			//method: "search_in_favorites",
			word: text
		};
		
		var onItemsLoaded = function(xml : Xml) {
			var parser = new ItemXmlParser(xml);
			onLoad(parser.parse());
		};
		
		this.request("search_in_favorites", params, onItemsLoaded, onError);
	}
	
	public function deleteElementFromFavorites(id : Int) {
		var params = {
			//method : "delete_from_favorites",
			channel : id
		};
		
		var stub = function (xml : Xml) {
			
		};
		
		this.request("delete_from_favorites", params, stub, onError);
	}
	
	public function updtTime(pr : String, onLoad : String -> Void) {
		var params = {
		//	method : "update_time",
			prev : pr
		};
		
		var onResult = function(xml : Xml) {
			var iter = xml.iterator();
			var fc = xml.elementsNamed("response");
			var resp = fc.next();
			onLoad(cast(resp.firstChild()));
		};
		this.request("update_time", params, onResult, onError);
	}
		
		
	private function onError(s : String) : Void
	{
		trace(s);
	}
	
}